//
//  QKReuseCtrl.m
//  testTool
//
//  Created by Su on 2018/12/9.
//  Copyright © 2018 com.qiankun. All rights reserved.
//

#import "QKReuseCtrl.h"
#import "QKReuseTableView.h"
#import "QKTimeGroupModel.h"

@interface QKReuseCtrl ()<UITableViewDelegate,UITableViewDataSource,QKReuseTableViewDataSource>
{
    QKReuseTableView *tableview;
    UIButton *reloadButton;
    NSMutableArray *dataSource;
}

@end

@implementation QKReuseCtrl

-(void)injected{
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.redColor;
    
    tableview = [[QKReuseTableView alloc] initWithFrame:CGRectMake(0, 0, kEVNScreenWidth, kEVNScreenHeight-60) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.indexDataSource = self;
    [self.view addSubview:tableview];
    
    
    //创建一个按钮
    reloadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, kEVNScreenHeight-66, self.view.frame.size.width, 40)];
    reloadButton.backgroundColor = [UIColor redColor];
    [reloadButton setTitle:@"reloadTable" forState:UIControlStateNormal];
    [reloadButton addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:reloadButton];
    
    
    dataSource = [NSMutableArray array];
    for (int i = 0; i < 100; i++) {
        [dataSource addObject:@(i+1)];
    }

}





-(NSArray<NSString *> *)indexTitleFromReuseTable:(UITableView *)tableview{
    //奇数次调用返回6个字母，偶数次调用返回11个
    static BOOL change = NO;
    
    if (change) {
        change = NO;
        return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K"];
    }
    else{
        change = YES;
        return @[@"A",@"B",@"C",@"D",@"E",@"F"];
    }
}
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"reuseId";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //如果重用池当中没有可重用的cell，那么创建一个cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    // 文案设置
    cell.textLabel.text = [[dataSource objectAtIndex:indexPath.row] stringValue];
    
    //返回一个cell
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)doAction:(id)sender{
    NSLog(@"reloadData");
    [tableview reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
